//*************************************************
// Filename: create_league.js
// 
// This JavaScript file contains a function for
// checking and validating the fields contained
// in the create_league.php page.
//
//
// Author: Caleb Goff
// Date Created: 10/10/2009
//*************************************************

/**
 * @Desc Function called on a POST.
 *
 */
function postLoad(){
	resetElems();
}

/**
 * @Desc Resets all of the elements with the class name 'resets'.
 */
function resetElems(){

	var elems = document.getElementsByClassName("resets");

	for (var i = 0; i < elems.length; i++)
		elems[i].value = "";

}
/**
* @Desc Checks and validates the create league form fields.
*
**/
function checkCreateLeagueFields() {

	var league_name           = document.getElementById("league_name").value;
	var league_pwd            = document.getElementById("league_password").value;
    var confirm_league_pwd    = document.getElementById("confirm_league_password").value;
	var draft_buttons         = document.draft_form.draft_group;
	var draft_type            = '';
	var draft_time            = document.getElementById("league_draft").value;
	var draft_date            = new Date(draft_time);
	var email_invites         = document.getElementById("league_invites").value;
	var emails;
	var league_pwd_hash;

    if(checkInput(league_name) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    
    if(checkInput(league_pwd) == false){
	    Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
	    return;    
    }
    
    if(checkInput(confirm_league_pwd) == false){
	    Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
	    return;    
    }
      
    if(checkInput(email_invites) == false){
        Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
        return;    
    }      
    
	// Perform input checks
	var curDate = new Date();

    // If date input is invalid
	if (draft_date < curDate)
	{
		Popup.showModal('popupBadDraftDate',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}

    // If league name is too short
	if (league_name.length < 4) {
		Popup.showModal('popupBadLeagueName',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}

    // If password fields don't match, or invalid length
	if (league_pwd.length < 6 || league_pwd != confirm_league_pwd) {
		Popup.showModal('popupBadLeaguePwd',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	} else {
		league_pwd_hash = getHash(document.getElementById("league_password"));
	}
	
    // Retrieve draft type from the radio buttons
	for (i=0; i < draft_buttons.length; i++) {
		if (draft_buttons[i].checked == true) {
			draft_type = draft_buttons[i].value;
		}
	}

    // If draft date is less than 10 characters
	if (draft_date.length < 10) {
		Popup.showModal('popupBadDraftDate',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}

    // If email invites is invalid length
	if (email_invites.length < 10) {
		Popup.showModal('popupBadInvites',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	} else {
        // Cycle through emails and validate them
        var email_array = email_invites.split(';');
        for(var i = 0;i < email_array.length;i++) {
            if (echeck(email_array[i]) == false) {
                Popup.showModal('popupBadInvites',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
                return;
            }
        }
    }
	// End input checks

	// Send these login paramaters to the PHP function on the server using
	// an HTTP post request
	var paramNames = "leagueName,leaguePwd,draftType,emails,draftDate,leaguePwdHash";
	var paramVals  = league_name + "," + league_pwd + "," + draft_type + "," + email_invites + "," + draft_date 
                     + "," + league_pwd_hash;
	var url        = "create_league.php";

	// Wait for a response from the login.php file
	var response = postRequest(paramNames, paramVals, url);

	// Check if login was successful
	if (response == 1){
		Popup.showModal('popupLeagueNameExists',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
	}
	else{
		//Route the owner to the team creation page
		window.location = "team_creation.php";
	} 
}
